#include "../include/head.h"
int set_arg(char *line, char *arg)
{
    //将指针偏移到字符=
    char *ptr = strchr(line, '=');
    ERROR_CHECK(ptr, NULL, "conf_file error");

    //= 后面的字符串
    strcpy(arg, ptr + 2);
    return 0;

}

int conf_get(char *conf, char *ip, int *port)
{
    FILE *fp = fopen(conf, "r");
    ERROR_CHECK(fp, NULL, "failed to open");

    char line[128] = "";
    char buf[128] = "";
    
    //得到ip fgets第三个参数才是 FILE*
    if (fgets(line, sizeof(line), fp) == NULL) {
        fprintf(stderr, "Failed to read IP line!\n");
        fclose(fp);
        return -1;
    }
    
    line[strlen(line) - 1] = '\0';
    if (set_arg(line, ip) != 0) {
        fclose(fp);
        return -1;
    }
    
    //得到port
    memset(buf, 0, sizeof(buf));
    memset(line, 0, sizeof(line));
    if (fgets(line, sizeof(line), fp) == NULL) {
        fprintf(stderr, "Failed to read port line!\n");
        fclose(fp);
        return -1;
    }
    line[strlen(line) - 1] = '\0';
    if (set_arg(line, buf) != 0) {
        fclose(fp);
        return -1;
    }
    *port = atoi(buf);
    
    fclose(fp);
    return 0;

}
